package com.java.computeronline.model.blo;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.java.computeronline.model.entity.Customer;
import com.java.computeronline.model.blo.PMF;

public class CustomerBLO {
	
	@SuppressWarnings("unchecked")
	public List<Customer> list() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Customer> customers = null;
		Query query = pm.newQuery(Customer.class, "deleted == value");
		query.declareParameters("Integer value");
		customers = (List<Customer>)query.execute(0);
        return customers;
	}

	public Customer add(Customer customer) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
            pm.makePersistent(customer);
        } finally {
            pm.close();
        }
		return customer;
	}

	public Customer delete(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Customer customer;
		try {
            customer = pm.getObjectById(Customer.class, id);
            pm.deletePersistent(customer);
        } finally {
            pm.close();
        }
		return customer;
	}

	public Customer update(Customer cus){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Customer customer;
		try {
            customer = pm.getObjectById(Customer.class, cus.getId());
            customer.setName(cus.getName());
    		customer.setPassword(cus.getPassword());
    		customer.setAddress(cus.getAddress());
    		customer.setEmail(cus.getEmail());
    		customer.setGender(cus.getGender());
    		customer.setPhone(cus.getPhone());
    		customer.setLastModified(new Date());
            pm.makePersistent(customer);
        } finally {
            pm.close();
        }
		return customer;
	}
}
